import { exec } from "child_process";
import { session } from "electron";
import { join } from "path";
export const isDev = () => {
  return process.env.NODE_ENV === "development";
};

export const PATH = {
  public: join(__dirname, isDev() ? "../public" : "./"),
  url: `http://localhost:8000`,
  indexHtml: join(__dirname, "index.html"),
  preload: join(__dirname, "preload.js"),
  iconfont: isDev() ? "/iconfont.js" : join(__dirname, "iconfont.js"),
  dataPath:`D:\\ProgramData\\SmartPerf_Editor`,
};

export const catchWeb = (
  win,
  webRequest = session.defaultSession.webRequest
) => {
  webRequest.onBeforeRequest(
    {
      urls: ["http://127.0.0.1:5500/*"], // 拦截url规则设置,
    },
    (details, next) => {
      if (details.url.includes("?code=")) {
        win.webContents.send("login-webRequest", details.url);
      } else {
        win.webContents.send("logout-webRequest", details.url);
      }
    }
  );
};

export const cmdExec = (cmd) => {
  return new Promise((resolve, reject) => {
    exec(
      cmd,
      { cwd: join(process.cwd(), isDev() ? "./plugins/" : "./") },
      (error, stdout, stderr) => {
        if (error) {
          reject(error);
          return;
        }
        resolve(stdout);
      }
    );
  });
};
